import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from "@/views/Home-component.vue";
import Login from '@/views/Login-component.vue';
import Register from '@/views/Register-component.vue'
import ForgetPassword from "@/views/ForgetPassword-component.vue";
import jwt_decode from "jwt-decode";


Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'Login',
        component: Login
    },
    {
        path: '/home',
        name: 'Home',
        component: Home,
        beforeEnter: (to, from, next) => {
            const usrToken = jwt_decode(localStorage.getItem('token'));
            if (usrToken) {
                next();
            } else {
                next('/login')
            }
        }
    },
    {
        path: '/register',
        name: 'Register',
        component: Register
    },
    {
        path: '/forgetPassword',
        name: 'ForgetPassword',
        component: ForgetPassword
    }
]

const router = new VueRouter({
    routes
})

export default router
